﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;

namespace MessageHubs
{
    public class IoTPersistentConnection : PersistentConnection
    {
        private static long connections = 0;
        /// <summary>
        /// 新客户端连接时触发
        /// </summary>
        /// <param name="request"></param>
        /// <param name="connectionId"></param>
        /// <returns></returns>
        protected override async Task OnConnected(IRequest request, string connectionId)
        {
            Interlocked.Increment(ref connections);

            //给特定”订阅“了Connection.DefaultSignal标志的客户端
            //ConnectionMessage connectionMessage = new ConnectionMessage(Connection.DefaultSignal, "ConnectionMessage");
            //await Connection.Send(connectionId, connectionMessage);

            #region 登录管理
            //在数据库或缓存中存储新的客户端连接
            //await _service.SaveNewConnectionAsync(connectionId);
            #endregion

            #region 组管理
            //添加到组
            //await Groups.Add(connectionId, "groupName");
            //向特定组发送消息
            //await Groups.Send("groupName", message);
            #endregion

            //发送ArraySegment<byte>类型数据将不被序列化
            await Connection.Send(connectionId, "Welcome, " + connectionId);
            await Connection.Broadcast(connectionId + " Connected", connectionId);
            await base.OnConnected(request, connectionId);
        }
        /// <summary>
        /// 客户端断开连接或连接超时，时触发
        /// </summary>
        /// <param name="request"></param>
        /// <param name="connectionId"></param>
        /// <param name="stopCalled"></param>
        /// <returns></returns>
        protected override async Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
        {
            Interlocked.Decrement(ref connections);
            await Connection.Broadcast(connectionId + " Disconnected", connectionId);
            await base.OnDisconnected(request, connectionId, stopCalled);
        }
        /// <summary>
        /// 收到客户端的数据时触发
        /// </summary>
        /// <param name="request"></param>
        /// <param name="connectionId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected override async Task OnReceived(IRequest request, string connectionId, string data)
        {
            //var message = JsonConvert.DeserializeObject<string>(data);

            await Connection.Broadcast(connectionId +":"+ data, connectionId);

            //await base.OnReceived(request, connectionId, data);
        }
        /// <summary>
        /// 客户端重新连接时触发
        /// </summary>
        /// <param name="request"></param>
        /// <param name="connectionId"></param>
        /// <returns></returns>
        protected override async Task OnReconnected(IRequest request, string connectionId)
        {
            await base.OnReconnected(request, connectionId);
        }
        /// <summary>
        /// 重新加入组时触发
        /// </summary>
        /// <param name="request"></param>
        /// <param name="groups"></param>
        /// <param name="connectionId"></param>
        /// <returns></returns>
        protected override IList<string> OnRejoiningGroups(IRequest request, IList<string> groups, string connectionId)
        {
            return base.OnRejoiningGroups(request, groups, connectionId);
        }

        protected override bool AuthorizeRequest(IRequest request)
        {
            if (request.User != null && request.User.Identity.IsAuthenticated)
            {
                request.User.IsInRole("admin");
                return true;
            }
            return false;
            //return base.AuthorizeRequest(request);
        }
    }
}